package com.iter.devbox.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 点对点消息生产者
 * 
 * @author 杨利军
 */
public class QueueProducer {

	/**生产者发送消息*/
	public void send () {
		try {
			//点对点消息的发送
			ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
			Connection connection = connectionFactory.createConnection();
			connection.start();
			Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);//acknowledge承认
			Destination destination = session.createQueue("messageQueue");
			MessageProducer producer = session.createProducer(destination);
			producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			ObjectMessage objectMessage = session.createObjectMessage("[queue] Hello World!");
			producer.send(objectMessage);
			session.commit();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}

